fix: report blocks() in 512-byte units per POSIX stat(2)#348
Draft
toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
Draft
fix: report blocks() in 512-byte units per POSIX stat(2)#348toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
Conversation
blocks() was computing the number of filesystem blocks (ceil(size/blksize)) but stat(2) st_blocks is always reported in 512-byte units regardless of st_blksize. A 5-byte file with blksize=4096 should report blocks=8, not 1. Updated blocks() to multiply by (blksize/512) and fixed tests that encoded the incorrect expectations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
blocks()now returns st_blocks in 512-byte units, matching real POSIXstat(2)behavior.Why
The previous implementation returned the count of filesystem blocks (
ceil(size/blksize)) — butstat(2)always reportsst_blocksin 512-byte units regardless ofst_blksize. A 5-byte file withblksize=4096should havestat[12] = 8(one 4096-byte block = 8 × 512), not1. Any code under test relying on$stat[12]for space calculations (e.g.,du, quota checks,File::Find::Rule) would get wrong answers from mocked files.How
Multiply the filesystem block count by
blksize / 512. Updatedt/blocks.t,t/Test-MockFile_file.t, andt/mock_stat.tto assert the correct values.Testing
Full test suite passes (94 files, 1587 tests). Only pre-existing
t/fh-ref-leak.tfailure (GH #179).Verified against real Perl:
stat()on a 5-byte tempfile returnsblocks=8withblksize=4096.🤖 Generated with Claude Code